package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itheima.POJO.OldHand;
import com.itheima.POJO.Order;
import com.itheima.POJO.ReceiveOrder;
import com.itheima.POJO.ShunDai;
import com.itheima.service.IOldHandService;
import com.itheima.service.IOrderService;
import com.itheima.service.IReceiveOrderService;
import com.itheima.service.IShunDaiService;
import com.itheima.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zqystart
 * @create 2022-09-01-11:26
 */
@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private IOldHandService oldHandService;

    @Autowired
    private IOrderService orderService;


    @GetMapping("/save")
    public R save(int id){//保存二手交易物品的订单
        OldHand oldHand = oldHandService.getById(id);
        Order order=new Order();
        order.setOldHand(oldHand);
        order.setOldHandUsername(oldHand.getUsername());
        order.setOrderNo(StringUtil.produce());
        order.setOldHandGoodsInfo(oldHand.getGoodsInfo());
        order.setOldHandImage(oldHand.getImage());
        order.setOldHandMoney(oldHand.getMoney());
        order.setOldHandTel(oldHand.getTel());
        boolean save = orderService.save(order);
        Map<String,Object> map=new HashMap<>();
        map.put("message",save);
        return R.ok(map);
    }

//    @GetMapping("/selectAll")
//    public R selectAll(){//查询所有的二手交易的货物的订单
//        List<Order> list = orderService.list();
//        for (Order order : list) {
//            OldHand oldHand = oldHandService.getById(order.getGoodsId());
//            order.setOldHand(oldHand);
//        }
//        Map<String,Object> map=new HashMap<>();
//        map.put("message",list);
//        return R.ok(map);
//    }

    @GetMapping("/selectByUsername")
    public R selectByUsername(String username){//通过用户名查询所有的二手交易的货物的订单
        List<Order> list = orderService.list(new QueryWrapper<Order>().eq("oldHandUsername", username));
        Map<String,Object> map=new HashMap<>();
        map.put("message",list);
        return R.ok(map);
    }
}
